﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Grizzy_X
{
    public class CMap
    {
        /// <summary>
        /// 内部码，ID为该地图的ID号，由系统自动生成
        /// </summary>
        private int ID;
        /// <summary>
        /// 地图的名字，由用户指定，缺省值为New Map
        /// </summary>
        public string Name;
        public string MapPath;
        /// <summary>
        /// 记录该地图内包含的所有图层，即为点、线、面和文本图层中的一种
        /// </summary>
        public List<CLayer> Layer_List;
        public List<CBaseMap> BaseMap_List;
        /// <summary>
        /// 记录该地图的比例尺，默认取所有图层中的最小值
        /// </summary>
        public int Scale;
        

        /// <summary>
        /// 记录该地图内包含的所有图层的文件路径
        /// </summary>
        public List<string> Layer_Path;

        /// <summary>
        /// 记录该地图内包含的所有图层的显示顺序
        /// </summary>
        public List<int> Layer_Order;

        /// <summary>
        /// 记录地图的像素宽高???应为实际的
        /// </summary>
        public double Width;
        public double Height;

        public string MapDes;
        public string Unit;
        public List<int> Base_Order;
        /// <summary>
        /// 新建一个地图，返回新建是否成功
        /// </summary>
        public bool New(string mapname,double mapheight,double mapwidth,string mappath,string mapdes,string unit)
        {
            try
            {
                Name = mapname;
                Height = mapheight;
                Width = mapwidth;
                MapDes = mapdes;
                MapPath = mappath;
                Unit=unit;
                Layer_List = new List<CLayer>();
                Layer_Path = new List<string>();
                Layer_Order = new List<int>();
                Base_Order = new List<int>();
                BaseMap_List = new List<CBaseMap>();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 在该地图中添加一个层，返回添加是否成功
        /// </summary>
        public bool Add(CLayer newlayer)
        {
            try
            {
                Layer_List.Add(newlayer);
                Layer_Path.Add(newlayer.GetPath());
                Layer_Order.Insert(0, newlayer.GetID());
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 在该地图中移除一个层，返回移除是否成功
        /// </summary>
        public CLayer FindID(int ID)
        {
            for (int i = 0; i < Layer_List.Count; i++)
            {
                if (Layer_List[i].GetID() == ID)
                    return Layer_List[i];
            }
            return null;
        }

        /// <summary>
        /// 改变该地图图层显示顺序，返回操作是否成功
        /// </summary>

        /// <summary>
        /// 将地图导出为jpg或其他格式栅格图像，返回操作是否成功
        /// </summary>
        ///
        public void ModifyLayerOrder()
        {
            Layer_Order.Clear();
            foreach (TreeNode child in MainForm.pMainWin.LayerTree.Nodes[0].Nodes)
            {
                Layer_Order.Add(Convert.ToInt16(child.Name.Replace("childnode", "")));
            }
        }
        public void ModifyBaseOrder()
        {
            Base_Order.Clear();
            foreach (TreeNode child in MainForm.pMainWin.LayerTree.Nodes[1].Nodes)
            {
                Base_Order.Add(Convert.ToInt16(child.Name.Replace("childnode", "")));
            }
        }
        public void RefreshLayerOrder(Childform f)
        {
            if (Controller.CountLayer == 0)
                return;
            PictureBox TempF = new PictureBox();
            
            for (int i = 1; i <= Controller.CountLayer; i++)
            {
                if (i == 1&&Controller.CountBase==0)
                    TempF = (PictureBox)(f.panelbase.Controls.Find("Layer1", false))[0];
                else if (i == 1 && Controller.CountBase > 0)
                {
                    TempF = Controller.FindBase(Controller.MyMap.BaseMap_List[Controller.MyMap.Base_Order[0] - 1].ID);
                    TempF = (PictureBox)(TempF.Controls.Find("Layer1", false))[0];
                }
                else
                    TempF = (PictureBox)(TempF.Controls.Find("Layer" + i.ToString(), false))[0];
                //TempF.Refresh();
                if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1].bVisible == false)
                {
                    TempF.Image = Controller.Blank;
                    continue;
                }
                if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1].Layer_Type == CLayer.LayerType.CPointLayer)
                    TempF.Image = ((CPointLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1]).ptlayer;
                else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1].Layer_Type == CLayer.LayerType.CLineLayer)
                    TempF.Image = ((CLineLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1]).lnlayer;
                else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1].Layer_Type == CLayer.LayerType.CPolygonLayer)
                    TempF.Image = ((CPolygonLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1]).pnlayer;
                else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1].Layer_Type == CLayer.LayerType.CTextLayer)
                    TempF.Image = ((CTextLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[Controller.CountLayer - i] - 1]).txlayer;
            }
        }
        public void RefreshBaseOrder(Childform f)
        {
            if (Controller.CountBase == 0)
                return;
            PictureBox TempF = new PictureBox();
            for (int i = 1; i <= Controller.CountBase; i++)
            {
                if (i == 1)
                    TempF = (PictureBox)(MainForm.f1.panelbase.Controls.Find("Base1", false))[0];
                else
                    TempF = (PictureBox)(TempF.Controls.Find("Base" + i.ToString(), false))[0];
                if (Controller.MyMap.BaseMap_List[Controller.MyMap.Base_Order[Controller.CountBase - i] - 1].Visible == false)
                {
                    TempF.Image = Controller.Blank;
                    continue;
                }
                TempF.Image = Controller.MyMap.BaseMap_List[Controller.MyMap.Base_Order[Controller.CountBase - i] - 1].basebmp;
            }
        }
    }
}
